package com.wuyixu.controller;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wuyixu.domain.Category;
import com.wuyixu.domain.Sku;
import com.wuyixu.domain.Spu;
import com.wuyixu.domain.SpuVo;
import com.wuyixu.service.CategoryService;
import com.wuyixu.service.SkuService;
import com.wuyixu.service.SpuService;

@Controller
public class IndexController {
	@Reference
	private SpuService spuService;
	@Reference
	private CategoryService categoryService;
	@Reference
	private SkuService skuService;
	
	
	@RequestMapping({"/","index",""})
	public String index(Model model,SpuVo spuVo){
		spuVo.setPageSize(10);
		PageInfo<Spu> pageInfo = spuService.list(spuVo);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("spuVo", spuVo);
		return "index";
	}
	
	@RequestMapping("catData")
	@ResponseBody
	public List<Category> getData(){
		// 获取到所有分类的数据
		 List<Category> categories = categoryService.list(0);
		 return categories;
		
	}
	@RequestMapping("spu")
	public String spuDetail(Model model,int spuId) {
		
		// 获取spu 的信息
		Spu spu = spuService.getById(spuId);
		// 获取sku的列表
		List<Sku> skuList = skuService.listDetailBySpu(spuId);
		System.out.println("spu is " + spu);
		System.out.println("sku is " + skuList);
		model.addAttribute("spu", spu);
		model.addAttribute("skuList", skuList);
		return "spudetail";
	}
}
